const Router = require('koa-router')
const { koaBody } = require('koa-body')

// 创建地址为‘role/xxx’的api地址
const router = new Router({ prefix: '/role' })

// 导入中间件
const { auth } = require('../middleware/auth.middleware')
const {
    roleAddValidator,
    roleCodeIsExist,
    verifyCodeOnly,
    verifyAdminRole,
    roleModifyValidator,
    menuIsExist
} = require('../middleware/role.middleware')

// 导入controller层
const {
    getMenuListByRole,
    addRole,
    modifyRoleInfo,
    deleteRole,
    getRoleList,
    modifyMenu,
    getAllMenuList
} = require('../controller/role.controller')


// 按照角色权限获取菜单
router.get('/getMenu', koaBody({ multipart: true }), auth, roleCodeIsExist, getMenuListByRole)
// 获取全部菜单项
router.get('/getAllMenu', koaBody({ multipart: true }), auth, getAllMenuList)
// 修改菜单权限
router.post('/modifyMenu', koaBody({ multipart: true }), auth, verifyAdminRole, roleCodeIsExist, menuIsExist, modifyMenu)
// 获取角色列表
router.get('/getRoleList', koaBody({ multipart: true }), auth, getRoleList)
// 新增角色
router.post('/addRole', koaBody({ multipart: true }), auth, verifyAdminRole, roleAddValidator, verifyCodeOnly, addRole)
// 修改角色信息
router.post('/modifyRoleInfo', koaBody({ multipart: true }), auth, verifyAdminRole, roleModifyValidator, modifyRoleInfo)
// 按照角色编码删除角色
router.post('/deleteRole', koaBody({ multipart: true }), auth, verifyAdminRole, roleCodeIsExist, deleteRole)

module.exports = router